<!--
 * @Author: 郭安东
 * @Date: 2021-03-12 16:51:04
 * @LastEditTime: 2021-04-11 19:13:40
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: \test\src\views\function\temperature.vue
-->
<template>
  <div class="temperature">
    <el-card class="box-card">
      <el-link class="userName">{{ userName }}</el-link>
      <el-tag :type="statesType" style="margin-left: 30px">{{ states }}</el-tag>
      <span>
        <el-button class="loginOut-btn" @click="dialog = true"><i class="el-icon-switch-button"></i></el-button>
      </span>
    </el-card>
    <four-menu :buts-data="dataFourMenu" :title=" '体温管理' " class="four-menu"/>
    <div v-show="user === 1">
      <el-card class="box-card">
        <div class="form-box">
          <el-form ref="form" :model="form" label-width="80px">
            <el-form-item label="用户名">
              <el-input v-model="form.userName"></el-input>
            </el-form-item>
            <el-form-item label="体温">
              <el-input v-model="form.temperature"></el-input>
            </el-form-item>
            <el-form-item class="form-box-btn">
              <el-button type="primary">保存</el-button>
              <el-button>取消</el-button>
            </el-form-item>
          </el-form>
        </div>
      </el-card>
    </div>
    <el-dialog
      title="确定注销用户吗"
      :visible.sync="dialog"
      width="30%"
      center>
      <span slot="footer" class="dialog-footer">
        <el-button type="primary" @click="loginOut()">确 定</el-button>
        <el-button @click="dialog = false">取 消</el-button>
      </span>
    </el-dialog>
    <div v-show="user === 0">
      <el-card class="box-card">
        <div class="searchBox">
          <el-form :inline="true" :model="searchForm" class="demo-form-inline">
            <el-form-item>
              <el-button @click="centerDialogVisible = true" v-show="role === 'role_user'">
                增加
              </el-button>
            </el-form-item>
            <el-form-item label="按名字查询：" v-show="role === 'role_admin'">
              <el-input placeholder="请输入内容" v-model="searchForm.userName" class="searchInput"></el-input>
            </el-form-item>
            <el-form-item label="按日期查询：">
              <el-date-picker
                v-model="searchForm.date"
                type="date"
                placeholder="日期"
                class="searchInput">
              </el-date-picker>
            </el-form-item>
            <el-form-item>
              <el-button type="primary" round @click="searchTemp()">搜索</el-button>
              <el-button round @click="getTemperature()">重置</el-button>
            </el-form-item>
          </el-form>
        </div>
        <common-table
          ref="mainTableRef"
          :table-data="mainTableData"
          :table-label="mainTableLabel"
          :table-config="mainTableConfig"
          :default-sort="mainTableConfig.defaultSort"
          style="height: 480px; margin-top: 10px"
          @handle-size-change="handleSizeChange"
          @handle-current-change="handleCurrentChange">
          <template slot="operating" slot-scope="scope">
            <el-button class="operatingButton" @click="deleteUserTemperature(scope.row)" v-show="role === 'role_admin'">
              删除
            </el-button>
            <el-button class="operatingButton"  @click="updateTemperature(scope.row)" v-show="role === 'role_user'">
              修改
            </el-button>
          </template>
        </common-table>
      </el-card>
      <el-dialog
        title="增加体温信息"
        :visible.sync="centerDialogVisible"
        width="30%"
        center>
        <el-form ref="form" :model="form" label-width="80px">
            <el-form-item label="用户ID">
              <el-input v-model="form.userId" disabled></el-input>
            </el-form-item>
            <el-form-item label="体温">
              <el-input v-model="form.temperature"></el-input>
            </el-form-item>
            <el-form-item class="form-box-btn">
              <el-button type="primary" @click="insertTemperatureInfo()">保存</el-button>
              <el-button @click="centerDialogVisible = false">取消</el-button>
            </el-form-item>
          </el-form>
      </el-dialog>
      <el-dialog
        title="修改温度信息"
        :visible.sync="updateDialog"
        width="30%"
        center>
        <el-form ref="updateForm" :model="updateForm" label-width="80px">
            <el-form-item label="用户ID">
              <el-input v-model="updateForm.userId" disabled></el-input>
            </el-form-item>
            <el-form-item label="体温">
              <el-input v-model="updateForm.temperature"></el-input>
            </el-form-item>
            <el-form-item class="form-box-btn">
              <el-button type="primary" @click="insertTemperatureInfo()">保存</el-button>
              <el-button @click="updateDialog = false">取消</el-button>
            </el-form-item>
          </el-form>
      </el-dialog>
    </div>
  </div>
</template>
<script>
import commonTable from '../../components/common-table/index'
import FourMenu from '../../components/menu/four-menu'
import { getTemperatureInfo, insertTemperature, deleteTemperature, userLoginOut } from '../../api/apply-info'
export default {
  name: 'Temperature',
  components: { commonTable, FourMenu },
  data () {
    return {
      user: 0,
      userName: '',
      role: '',
      userId: '',
      states: '',
      statesType: '',
      centerDialogVisible: false,
      updateDialog: false,
      dialog: false,
      form: {
        userId: '',
        temperature: ''
      },
      searchForm: {
        userId: '',
        userName: '',
        date: '',
        pageSize: 10,
        currentPage: 1
      },
      updateForm: {
        userId: '',
        temperature: ''
      },
      searchData: {
        userId: '',
        date: '',
        userName: '',
        pageSize: 10,
        currentPage: 1
      },
      deleteData: {
      },
      index: 0,
      dataFourMenu: [{ name: '返回' }],
      mainTableData: [],
      mainTableLabel: [
        {
          prop: 'id',
          label: '编号',
          width: 125,
          sortable: true
        },
        {
          prop: 'userId',
          label: '用户Id',
          width: 125,
          sortable: true
        },
        {
          prop: 'userName',
          label: '用户名',
          width: 125,
          sortable: true
        },
        {
          prop: 'temperature',
          label: '体温',
          width: 125,
          sortable: true
        },
        {
          prop: 'date',
          label: '时间',
          width: 125,
          sortable: true
        },
        {
          prop: 'updateTime',
          label: '更新时间',
          width: 125,
          sortable: true
        },
        {
          prop: 'operating',
          label: '操作',
          fixed: 'right',
          width: 150,
          slot: true
        }
      ],
      mainTableConfig: {
        // 是否显示loading效果
        loading: false,
        // 是否开启分页
        pagination: true,
        // 当前页数
        page: 1,
        // 当前页条数
        count: 0,
        // 数据总条数
        totalCount: 0,
        // 每页显示条数
        // pageSize: 10,
        // 每页显示条数选择项
        // pageSizeOptions: [10, 20, 30, 40],
        // 是否开启隔行换色
        stripe: true,
        // 是否生成序号列
        orderNum: false,
        // 是否生成行选择框列
        rowSelect: true,
        // 表格唯一ID字段
        uniqueId: 'eventNo',
        // 表格高度，低于所有行高时会生成滚动条，高于所有数据的高度会留白
        tableHeight: '90%',
        // 填入表头高度和字体大小的样式对象
        headerSize: {
          height: '40px',
          'font-size': '15px'
        },
        // 填入表头背景色和字体色的样式对象
        headerColor: {
          color: '#333333',
          'background-color': '#e4eefc'
        },
        // 填入表格高度和字体大小的样式对象
        rowSize: {
          height: '35px',
          'font-size': '14px'
        },
        // 填入表格背景色和字体色的样式对象
        rowColor: {
          color: '#333333',
          'background-color': '#f7faff'
        },
        // 是否开启点击选中行
        clickRow: false,
        showTooltip: true
      }
    }
  },
  mounted () {
    this.getUserName()
    this.getTemperature()
  },
  methods: {
    // 获取账户id
    getUserName () {
      this.userName = sessionStorage.getItem('userName')
      this.form.userId = sessionStorage.getItem('userId')
      this.userId = sessionStorage.getItem('userId')
      this.role = sessionStorage.getItem('role')
      this.states = sessionStorage.getItem('states')
      if (this.states === '状态正常' || this.states === '管理员') {
        this.statesType = 'success'
      } else {
        this.statesType = 'danger'
      }
    },
    // 注销用户
    loginOut () {
      userLoginOut().then(res => {
        console.log('loginOut', res)
        this.dialog = false
        if (res.data.message === '体温数据填报成功') {
          this.$message({
            message: res.data.message,
            type: 'success'
          })
        } else {
          this.$message.error(res.data.message)
        }
        // 将用户名放入sessionStorage
        sessionStorage.setItem('userName', null)
        sessionStorage.setItem('userId', null)
        sessionStorage.setItem('role', null)
        // 将用户放入vuex
        this.$store.dispatch('setUser', null)
        this.$router.push({ path: '/login' })
      })
    },
    // 每页条数发生改变时触发
    handleSizeChange (val) {
      this.mainTableConfig.page = 1
      this.mainTableConfig.pageSize = val
      this.mainTableData = []
      this.getFaultTripTableData()
    },
    // 当前页发生改变时触发
    handleCurrentChange (val) {
      this.mainTableConfig.page = val
      this.searchData.currentPage = val
      this.getTemperature()
    },
    // 获取用户温度信息
    getTemperature () {
      if (this.role === 'role_admin') {
        this.searchData.userId = ''
      } else if (this.role === 'role_user') {
        this.searchData.userId = this.userId
      }
      getTemperatureInfo(this.searchData).then(res => {
        console.log('this res', res)
        this.mainTableData = res.data.data.records
        this.mainTableConfig.totalCount = res.data.data.total
        for (let i = 0; i < this.mainTableData.length; i++) {
          console.log('11')
          this.mainTableData[i].updateTime = this.mainTableData[i].updateTime.split('T')[1]
          this.mainTableData[i].updateTime = this.mainTableData[i].updateTime.split('.')[0]
          console.log(this.mainTableData[i].updateTime)
        }
      })
    },
    // 删除温度信息
    deleteUserTemperature (row) {
      // console.log('deleteUserTemperature', row)
      deleteTemperature(row).then(res => {
        // console.log('deleteTemperature', res)
        this.$message(res.data.message)
        this.getTemperature()
      })
    },
    // 填报温度信息
    insertTemperatureInfo () {
      insertTemperature(this.form).then(res => {
        console.log('this res', res)
        this.centerDialogVisible = false
        this.$message(res.data.message)
        this.getTemperature()
      })
    },
    // 修改温度信息
    updateTemperature (row) {
      this.centerDialogVisible = true
      this.updateForm.userId = row.userId
      this.updateForm.temperature = row.temperature
      console.log('row', row)
    },
    // 时间戳转换
    getNewTime (time) {
      const year = time.getFullYear()
      let mounth = time.getMonth() + 1
      mounth = mounth < 10 ? ('0' + mounth) : mounth
      let day = time.getDate()
      day = day < 10 ? ('0' + day) : day
      const Time = year + '-' + mounth + '-' + day
      return Time
    },
    // 搜索条件搜索
    searchTemp () {
      console.log('searchForm', this.searchForm)
      if (this.searchForm.date !== '') {
        this.searchForm.date = this.getNewTime(this.searchForm.date)
      }
      if (this.role === 'role_admin') {
        this.searchForm.userId = ''
      } else if (this.role === 'role_user') {
        this.searchForm.userId = this.userId
      }
      getTemperatureInfo(this.searchForm).then(res => {
        console.log('this res', res)
        this.mainTableData = res.data.data.records
      })
    }
  }
}
</script>
<style scoped>
.box-card {
    margin-top: 10px;
    margin-left: 10%;
    width: 1200px;
    border-radius: 20px;
    background-color: rgba(255, 255, 255, 0.8);
}
.userName{
  color: #7196b1;
  font-size: 16px;
  font-weight: bolder;
}
.loginOut-btn{
  float: right;
  margin-top: -5px;
  border-radius: 10px;
  color: #7196b1;
  border: none;
  background-color: rgb(240, 234, 234, 0.6);
}
.four-menu {
  margin-top: 10px;
  margin-bottom: 10px;
}
.form-btn {
  margin-left: 15%;
}
.form-box {
  width: 500px;
  margin-top: 10px;
  margin-left: 25%;
}
.form-box-btn {
  margin-left: 15%;
}
.searchBox {
  margin-left: 15%;
}
.searchInput {
  width: 200px;
  margin-right: 30px;
}
.el-form-item__label {
  text-align: right;
}
</style>
<style>
body{
  background-image: url('../../assets/bg3.jpeg');
  background-size: 100% 1000px;
  background-repeat: no-repeat;
}
.el-icon-switch-button {
  color: #e4252d;
  font-size: 17px;
  font-weight: bolder;
}
</style>
